package com.aurora.oa.work.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.DateUtil;
import com.aurora.oa.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DateTimePicker;

/**
 * Created by Administrator on 2017/4/28 0028.
 */
public class ApplyEvectionActivity extends BaseActivity {
    private static final int START_TIME = 1;
    private static final int END_TIME = 2;
    ProgressDialog progressDialog;
    RequestHttpModel model = new RequestHttpModelImpl();
    @Bind(R.id.tv_start_time)
    TextView tvStartTime;
    @Bind(R.id.tv_end_time)
    TextView tvEndTime;
    @Bind(R.id.et_evection_hour)
    EditText etEvectionHour;
    @Bind(R.id.et_evection_reson)
    EditText etEvectionReson;
    @Bind(R.id.et_remark)
    EditText etRemark;
    @Bind(R.id.bt_submit)
    Button btSubmit;
    DateTimePicker datePicker;
    private int selectDateType;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "出差申请", true);
        setMyContentView(R.layout.activity_apply_evection);
        ButterKnife.bind(this);
        initView();
    }

    @OnClick({R.id.tv_start_time, R.id.tv_end_time, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_start_time:
                selectDateType = START_TIME;
                datePicker.show();
                break;
            case R.id.tv_end_time:
                selectDateType = END_TIME;
                datePicker.show();
                break;
            case R.id.bt_submit:
                doSubmit();
                break;
        }
    }

    private void initView() {
        datePicker = new DateTimePicker(this,DateTimePicker.HOUR_24);
        datePicker.setLabel("年","月","日","时","分");
        datePicker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                switch (selectDateType){
                    case START_TIME:
                        tvStartTime.setText(year+"年"+month+"月"+day+"日"+" "+hour+":"+minute);
                        break;
                    case END_TIME:
                        tvEndTime.setText(year+"年"+month+"月"+day+"日"+" "+hour+":"+minute);
                        break;
                }
            }
        });

    }

    public void doSubmit() {
        if(tvStartTime.getText().toString().equals("")){
            ToastUtil.show(this,"请选择开始时间");
            return;
        }
        if(tvEndTime.getText().toString().equals("")){
            ToastUtil.show(this,"请选择结束时间");
            return;
        }

        String begintime = DateUtil.getDateString(tvStartTime.getText().toString());
        String endtime = DateUtil.getDateString(tvEndTime.getText().toString());
        String daytime = etEvectionHour.getText().toString().trim();
        String reason = etEvectionReson.getText().toString().trim();
        String remark = etRemark.getText().toString().trim();

        progressDialog = ProgressDialog.show(this, null, "正在提交数据...");
        Map<String, String> params = new HashMap<>();
        params.put("begintime", begintime==null?"":begintime);
        params.put("endtime", endtime==null?"":endtime);
        params.put("daytime", daytime==null?"":daytime);
        params.put("reason", reason==null?"":reason);
        params.put("remark", remark==null?"":remark);
        model.postForm(AuroraConstants.SEND_EVECTION_APPLY, params, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {

                Gson gson = new Gson();
                HttpResult result = gson.fromJson(body, HttpResult.class);
                progressDialog.dismiss();
                if (result != null && result.success) {
                    ToastUtil.show(getApplicationContext(), "提交成功!");
                    finish();
                } else {
                    ToastUtil.show(getApplicationContext(), result.msg);
                }
            }

            @Override
            public void onRequestDataFail(String message) {
                ToastUtil.show(getApplicationContext(), message);
                progressDialog.dismiss();
            }
        });
    }
}
